using System;
using System.Data;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization;
using System.Text;
using ICaveret;

namespace Shraga.BLL
{
	public class Cycle
	{
		#region Data Members
				
		#endregion

		#region Properties
		
		public int		CycleId { get; }
		public String   CycleName { get; }
		public String   Description { get; }
		public bool		IsActive { get; }		
		public DateTime StartDate { get; } 
		public DateTime EndDate { get; } 
		
		
		#endregion

		#region Constructors

		/// <summary>
		/// Loads a Cycle.
		/// </summary>
		/// <param name="CycleId"></param>
		public Cycle(int CycleId) {}

		/// <summary>
		/// Creates a new cycle
		/// </summary>
		/// <param name="CycleId"></param>
		public Cycle(String Name, String Description, DateTime StartDate, DateTime EndDate) {}

		#endregion

		#region Cycle Methods

		/// <summary>
		/// Sets the cycle as not active
		/// </summary>
		public void DeActivate() {}

		#endregion

		#region not used
		/*
						/// <summary>
						///load Cycle details 
						/// </summary>
						/// <param name="pernum"></param>
						private void loadCycleDetails(string spName, clsParameter[] param)
						{
		
							UniversalDB udb = new UniversalDB(Global.ConnectionString);				

							System.Data.DataTable dtPath;
				
							// runs the sql to get data
							dtPath = udb.RunSQLReturnDataTable(spName, ref param);

							// check if no data found
							if (dtPath.Rows.Count != 1)
							{
								throw new ShragaException(E_ERROR_ENUM.E_NOT_EXIST, Global.E_NOT_EXIST);
							}

							egion populate details
				
											c.cycle_name
											,p.name
											,p.ou_entity_id
											,p.id as path_id
											,p.Id
											,p.Description
											,p.Potential_sp_name
											,c.End_date
											,c.Start_date
											,c.Is_active
											,c.Cycle_id
						
							// reads the Path data

							//m_PathDetails.CycleDetails.CycleDetails = Convert.ToInt32(dtPath.Rows[0]["ID_NUMBER"]);

							m_PathDetails.Description = Convert.ToInt32(dtPath.Rows[0]["PNUMBER"]);
							m_PathDetails.NodeInPath = Convert.ToString(dtPath.Rows[0]["FIRST_NAME"]);
							m_PathDetails.PathId = Convert.ToString(dtPath.Rows[0]["LAST_NAME"]);
							m_PathDetails.PathName = Convert.ToString(dtPath.Rows[0]["SEX"]);
							m_PathDetails.Potential_SP_Name = Convert.ToString(dtPath.Rows[0]["CITIZENSHIP"]);
				
							ndregion
				
				
				
						}
			*/		

		#endregion
	}

	public class Path
	{
		#region Data Members
		
		public Hashtable m_lstPathNodes;

		#endregion
		
		#region Properties

		public int		 PathId { get; }
		public String	 PathName { get; }
		public String	 Description { get; }
		public String	 Potential_SP_Name { get; }
		public int		 Entity_OU_ID { get; }
		public int       CycleId { get; }
		
		public Hashtable Nodes { get; }
		public Hashtable Destinations {get;}

		#endregion

		#region Constructors

		/// <summary>
		/// Loads the Path.
		/// </summary>
		/// <param name="PathId"></param>
		public Path(int PathId)
		{
			try
			{
				LoadPathDetails(PathId);
				LoadNodes(PathId);
				LoadConnections(PathId);
			}
			catch
			{
				throw;
			}
		}
		
		/// <summary>
		/// Creates a new Path
		/// </summary>
		/// <param name="param1"></param>
		/// <param name="param2"></param>
		/// <param name="param3"></param>
		public Path(String param1, String param2, String param3) {}

		#endregion
		
		#region Private Path Methods

		/// <summary>
		///load Path details 
		/// </summary>
		private void LoadPathDetails(int PathID)
		{
			string spName = "Shraga_Path_Manage.Path_By_Id";
			clsParameter[] param  = new clsParameter[] {new clsParameter(Convert.ToInt32(PathID), OracleTypes.Int32)};
			UniversalDB udb = new UniversalDB(Global.ConnectionString);				

			// runs the sp to get data
			System.Data.DataTable dtPath = udb.RunStoredFunctionReturnDataTable(spName, ref param);

			// check if no data found
			if (dtPath.Rows.Count != 1)
			{
				throw new ShragaException(E_ERROR_ENUM.E_NOT_EXIST, Global.E_NOT_EXIST);
			}

			#region populate details
			//Populate cycle details
			m_PathDetails.cycleDetails = new Cycle(Convert.ToInt32(dtPath.Rows[0]["Cycle_id"]),
				Convert.ToDateTime(dtPath.Rows[0]["Start_date"]),
				Convert.ToDateTime(dtPath.Rows[0]["End_date"]),
				Convert.ToBoolean(dtPath.Rows[0]["Is_active"]),
				Convert.ToString(dtPath.Rows[0]["cycle_name"])).cycleDetails;

			// reads the Path data
			m_PathDetails.Description = Convert.ToString(dtPath.Rows[0]["Description"]);
			m_PathDetails.PathId = Convert.ToInt32(dtPath.Rows[0]["path_id"]);
			m_PathDetails.PathName = Convert.ToString(dtPath.Rows[0]["name"]);
			m_PathDetails.Potential_SP_Name = Convert.ToString(dtPath.Rows[0]["Potential_sp_name"]);
			m_PathDetails.Entity_OU_ID = Convert.ToInt32(dtPath.Rows[0]["ou_entity_id"]);
			#endregion
		}

		/// <summary>
		///load Path Nodes[] 
		/// </summary>
		private void LoadNodes(int PathID)
		{
			clsParameter[] param = new clsParameter[] {new clsParameter(Convert.ToInt32(m_PathDetails.PathId), OracleTypes.Int32)};
			UniversalDB udb = new UniversalDB(Global.ConnectionString);				

			//Load node details
			DataTable dtNodes = udb.RunStoredFunctionReturnDataTable("Shraga_Path_Manage.Path_Nodes", ref param);
				
			// check if no data found
			if (dtNodes.Rows.Count == 0)
				return;

			// load destinations
			DataTable dtDestinations = udb.RunStoredFunctionReturnDataTable("Shraga_Path_Manage.Path_Nodes_Destination", ref param);

			//load Node relations
			DataTable dtRelations = udb.RunStoredFunctionReturnDataTable("Shraga_Path_Manage.Path_Nodes_Relation", ref param);
				
			//load actions
			DataTable dtActions = udb.RunStoredFunctionReturnDataTable("Shraga_Path_Manage.Path_Nodes_Actions", ref param);

			#region populate details
		
			m_PathDetails.NodeDetails = new IShraga.NodeDetails[dtNodes.Rows.Count];
			m_PathDetails.NodeInterfaces = new ArrayList(dtNodes.Rows.Count);
			// reads the Path data
			for (int i=0; i < dtNodes.Rows.Count; i++)
			{
				Destination nodeDest = new Destination();
				string DestID = (Convert.ToString(dtNodes.Rows[i]["Destination_id"])=="") ? "0" : Convert.ToString(dtNodes.Rows[i]["Destination_id"]);
				DataRow[] currentNodeDest = dtDestinations.Select("DestID = " + DestID);
				if (currentNodeDest.Length > 0)
				{
					DataRow NodeDest = currentNodeDest[0];
					nodeDest = new Destination(
						Convert.ToInt32(NodeDest["DestID"]),
						Convert.ToString(NodeDest["DestName"]),
						Convert.ToString(NodeDest["Horaa"]),
						Convert.ToString(NodeDest["ProffName"]),
						Convert.ToInt32(NodeDest["ProffID"]),
						NodeDest["Design_male"] == DBNull.Value ? 0 : Convert.ToInt32(NodeDest["Design_male"]), 
						NodeDest["Design_female"]== DBNull.Value ? 0 : Convert.ToInt32(NodeDest["Design_female"]),
						NodeDest["Enlistment_date_male"]== DBNull.Value ? new DateTime(0) : Convert.ToDateTime(NodeDest["Enlistment_date_male"]),
						NodeDest["Enlistment_date_female"]== DBNull.Value ? new DateTime(0) : Convert.ToDateTime(NodeDest["Enlistment_date_female"]),
						NodeDest["Course_open_date"]== DBNull.Value ? new DateTime(0) : Convert.ToDateTime(NodeDest["Course_open_date"]),
						NodeDest["Course_end_date"]== DBNull.Value ? new DateTime(0) : Convert.ToDateTime(NodeDest["Course_end_date"]),
						NodeDest["Comments"]== DBNull.Value ? "" : Convert.ToString(NodeDest["Comments"]),
						NodeDest["Description"]== DBNull.Value ? "" : Convert.ToString(NodeDest["Description"]));
				}

				//ArrayList nodeSonIDs = new ArrayList();
				//ArrayList nodeFatherIDs = new ArrayList();
				//					DataRowCollection nodeSonIDs = new DataRowCollection();
				//					DataRowCollection nodeFatherIDs = new DataRowCollection();
				DataTable nodeFatherIDs = new DataTable();
				nodeFatherIDs.Columns.Add("ORIGION_NODE_IN_PATH");
				nodeFatherIDs.Columns.Add("DESTINATION_NODE_IN_PATH");
				nodeFatherIDs.Columns.Add("NAME");
				DataTable nodeSonIDs = new DataTable();
				nodeSonIDs.Columns.Add("ORIGION_NODE_IN_PATH");
				nodeSonIDs.Columns.Add("DESTINATION_NODE_IN_PATH");
				nodeSonIDs.Columns.Add("NAME");
				DataRow[] mySonIDs = dtRelations.Select("Origion_Node_in_Path = " + dtNodes.Rows[i]["Node_In_Path_Id"].ToString()); //I'm the origion - I'm the father
				DataRow[] myFatherIDs = dtRelations.Select("Destination_Node_in_Path = " + dtNodes.Rows[i]["Node_In_Path_Id"].ToString()); //I'm the dest - I'm the son

				for (int j=0;j<=mySonIDs.Length-1;j++)
				{
					DataRow mySon = mySonIDs[j];
					//I want to get the 2 Ids and the name of the dest
					nodeSonIDs.Rows.Add(new object[]{mySon.ItemArray[0],mySon.ItemArray[1],mySon.ItemArray[3]});
				}

				for (int j=0;j<=myFatherIDs.Length-1;j++)
				{
					DataRow myFather = myFatherIDs[j];
					//I want to get the 2 Ids and the name of the Origion
					nodeFatherIDs.Rows.Add(new object[]{myFather.ItemArray[0],myFather.ItemArray[1],myFather.ItemArray[2]});
				}

				NodeInPath myNode = new NodeInPath(
					dtNodes.Rows[i]["Node_In_Path_Id"] == DBNull.Value ? 0 : Convert.ToInt32(dtNodes.Rows[i]["Node_In_Path_Id"]),
					dtNodes.Rows[i]["Node_id"] == DBNull.Value ? 0 : Convert.ToInt32(dtNodes.Rows[i]["Node_id"]),
					dtNodes.Rows[i]["Node_Name"] == DBNull.Value ? "" : Convert.ToString(dtNodes.Rows[i]["Node_Name"]),
					dtNodes.Rows[i]["NodeType"] == DBNull.Value ? "" : Convert.ToString(dtNodes.Rows[i]["NodeType"]),
					dtNodes.Rows[i]["Description"] == DBNull.Value ? "" : Convert.ToString(dtNodes.Rows[i]["Description"]),
					dtNodes.Rows[i]["Is_abstract"] == DBNull.Value ? false : Convert.ToBoolean(dtNodes.Rows[i]["Is_abstract"]),
					dtNodes.Rows[i]["Path_Id"] == DBNull.Value ? 0 : Convert.ToInt32(dtNodes.Rows[i]["Path_Id"]),
					dtNodes.Rows[i]["Is_Need_Approve"] == DBNull.Value ? false : Convert.ToBoolean(dtNodes.Rows[i]["Is_Need_Approve"]),
					nodeDest,
					dtNodes.Rows[i]["Experation_Date"] == DBNull.Value ? new DateTime(0) : Convert.ToDateTime(dtNodes.Rows[i]["Experation_Date"]),
					dtNodes.Rows[i]["Experation_delta"] == DBNull.Value ? 0 : Convert.ToInt32(dtNodes.Rows[i]["Experation_delta"]),
					nodeFatherIDs,nodeSonIDs, new ArrayList());
				//Convert.ToInt32(dtNodes.Rows[i]["ID_NUMBER"])/*ArrlistNodeFater*/,
				//Convert.ToInt32(dtNodes.Rows[i]["ID_NUMBER"])/*ArrlistNodeSon*/,
				//Convert.ToInt32(dtNodes.Rows[i]["ID_NUMBER"])/*arrlistActions*/
					
				m_PathDetails.NodeDetails.SetValue(myNode.nodeDetails,i);
				m_PathDetails.NodeInterfaces.Add(myNode);
			}

			#endregion
				
		}

		/// <summary>
		///load Path Connections[]
		/// </summary>
		#endregion
		
		#region Public Path Methods

		public ArrayList GetAllDestinations()
		{
			/*			ArrayList destList = new ArrayList();
							foreach (IShraga.NodeDetails nd in m_PathDetails.NodeDetails)
							{
								if (nd.destination.DestName != null) destList.Add(nd.destination);
							}
							return destList;*/
		}

		public bool IsPathAcceptable()
		{
			return true;
		}

		public void CreatePotential()
		{
		}
		public ArrayList AcceptedCandidates()
		{
		}

		public DataTable GetStatistics() {}
		public Hashtable PathsTo() {}
		public Hashtable PathsFrom() {}
		public void AddCandidates(String CandidateIds) {}

		#endregion				
	}

	public class Node
	{
		#region Data Members

		#endregion

		#region Properties

		public int		 NodeId { get; }
		public String	 NodeName { get; }
		public String	 NodeType { get; }
		public String	 Description { get; }
		public bool		 IsAbstract { get; }
		public int		 PathId { get; }
		public bool		 IsNeedApprove { get; }
		public DateTime	 ExperationDate { get; }
		public int		 ExperationDelta { get; }
				
		public Hashtable NodeParents { get; }
		public Hashtable NodeSons { get; }
		public Hashtable ActionCommands { get; }
		public Hashtable Events { get; }
		public Hashtable Candidates { get; }

		#endregion

		#region Constructors

		/// <summary>
		/// Loads a NodeInPath.
		/// </summary>
		public Node(int NodeId) {}

		public Node(String param1, String param2, String param3) {}
		#endregion
	
		#region Node Methods

		public DataTable GetCandidates() {}
		public DataTable AddColumns(String Columns, DataTable Source) {}
		public void PerformAction(String CandidateIds, ActionCommand ActionToPerform) {}
		public void ProgressCandidates(String CandidateIds, Node NextNode) {}
		public void AddCandidates(String CandidateIds, Node PreviousNode) {}
		
		#endregion	
	}

	public class Event
	{
		#region Properties

		public int		EventId { get; }
		public DateTime EventDate { get; }
		public Place	EventPlace { get; }
		public DateTime Duration { get; }
		public DateTime BreakStartTime { get; }
		public DateTime BreakEndTme { get; }
		public int		TeamCount { get; }
		public bool		IsChangeable { get; }
		public DateTime UpdateDate { get; }
		public bool		IsCanceled { get; }		
	
		#endregion

		#region Public Event Methods
		
		public String		AddCandidates(String CandidateIds, Node EventNode) {}
		public void			RemoveCandidates(String CandidateIds, Node EventNode) {}
		public bool			AddCandidate(int CandidateId, DateTime Time, Node EventNode) {}
		public bool			MoveCandidate(int CandidateId, DateTime Time, Node EventNode) {}
		public DataTable	EventTimetable() {}
		public DateTime[]   GetFreeTimes() {}

		#endregion
	}
	
		
	public class Candidate
	{
		#region Data Members

		private Hashtable m_CandidacyDetails;
		private Hashtable m_HashtafotDetails;
		private Hashtable m_PotentialDetails;

		private int m_IdNumber;

		#endregion

		#region Properties

		public int		 CandidateId { get; }
		public String	 FirstName { get; }
		public String	 LastName { get; }		

		public Hashtable Candidacies { get; }		
		public Hashtable HashtafotHistory { get; }		
		public Hashtable Potentials { get; }		

		#endregion

		#region Constructors
			
		public Candidate(int IdNumber)
		{
			try
			{
				m_IdNumber = IdNumber;
			}
			catch
			{
				throw;
			}
		}

		#endregion

		#region Public Candidate Methods
	
		public void Hastefh(Destination NewDest, int ReasonId, int HashtafotTypeId, Destination Destination);
		public void ChangeCandidacy (Candidacy CandidacyToChange,int NewCandidacyStatus);

		#endregion

	}

	public class Candidacy
	{
		#region Data Members
		
		private int		  m_CandidacyId;
		private string	  PathName;
		private string	  Description;
		private string	  Status;
		private DateTime  StartDate;
		private DateTime  EndDate;
		private DateTime  StatusChangeDate;
		private Hashtable m_CandidateNode;
		private int		  m_IdNumber;
		
		#endregion

		#region Properties
		
		public int		 CandidacyId;
		public int		 CandidateId;
		public int  	 PathId;
		public int	     StatusId;
		public String    Status;
		public DateTime  StartDate;
		public DateTime  EndDate;
		public DateTime  StatusChangeDate;
		
		public Hashtable CandidateTrace;				

		#endregion

		#region Constructors
			
		public Candidacy(int CandidacyId)
		{
		}


		#endregion

		#region Public Candidacy Methods

		
		#endregion
	}

	public class CandidateNode
	{
		#region Properties

		public int		CandidateNodeId;
		public int		CandidacyId;
		public int		NodeId;
		public bool		IsCurrent;
		public bool		WasPresent;
		public DateTime EnterDate;
		public DateTime UpdateDate;
		public bool		Cancel;
		public int		EventId;
		public DateTime ExitDate;
		public DateTime Hour;
		public String	Comment;
		public String	ExitComment;
		public bool		IsArrivingApproved;
		public DateTime ArrivingApprovedDate;
		public int		PreviousNodeId;

		public Hashtable Grades;
		public Hashtable Actions;
		public Hashtable Surveys;

		#endregion
	}

	
	public class Survey
	{
		#region Data Members
		#endregion

		#region Properties
		
		public int		SurveyId { get; }
		public int		CandidateNodeId { get; }
		public int		SurveyTypeId { get; }
		public String	SurveyType { get; }
		public DateTime StartDate { get; }
		public DateTime EndDate { get; }
		public DateTime UpdateDate { get; }
		public DateTime InsertDataDate { get; }
		public bool     Cancel { get; }
		
		public Hashtable Fields  { get; }
		
		#endregion

		#region Constructors
		
		public Survey(int SurveyId) {}		
		public Survey(int CandidateNodeId, int SurveyType, DateTime StartDate, DateTime EndDate) {}
		#endregion

		#region Public Survey Methods

		public DataTable GetSurvey() {}
		public DataTable GetSurvey(DataTable FieldNames) {}
		public void FillSurvey(DataTable FieldsData) {}
		
		#endregion

		#region Public Static Survey Methods
		
		public DataTable GetSurveys(String CandidateIds, int NodeId, int SurveyType, DataTable FieldNames) {}
		#endregion
	}


	public class Action
	{
		#region Properties
		
		public int		ActionStatusId;
		public int		CandidateNode;
		public String	Description;
		public bool		IsDone;
		public int		OutputId;
		public DateTime OutputDate;

		#endregion

		#region Constructors

		public Action(int ActionStatusId) {}

		#endregion

		#region Public Action Methods
			
		public void PerformAction(ActionCommand ToPerform) {}

		#endregion
	}

	public class ActionCommand 
	{
		#region Properties

		public int		ActionId;
		public int		NodeId;
		public int		ActionTypeId;
		public String   ActionTypeName;
		public bool		IsOnTimeOut;
		public int		LetterFormatID;
		public int		Order;
		public int		ProcedureName;	

		#endregion

		#region Constructors
		
		public ActionCommand(int ActionId) {}

		#endregion
	}

	public class Hashtafa
	{
		#region Properties

		public int		HashtafaId;
		public int		CandidateId;
		public int		HashtafaStatusId;
		public DateTime HashtafaDate;
		public int		ReasonTypeId;
		public int		NewDestinationId;
		public int		OldDestinationId;
		public DateTime	DraftDate;
		public String   Comment;

		#endregion

		#region Constructors

		public Hashtafa(int HashtafaId) {}
		public Hashtafa(int CandidateId, int HashtafaStatusId, int ReasonTypeId, 
						int NewDestinationId, int OldDestinationId, DateTime DraftDate, String Comment) {}

		#endregion
		
	}

	public class CandidatesList : ArrayList
	{
		#region Constructors
		
		public CandidatesList(String CandidateIds)
		{
			foreach (String Id in CandidateIds.Split(new char[] {','}))
			{
				this.Add(Id);	
			}
		}

		#endregion		

		#region Public CandidatesList Methods	

		public override string ToString()
		{
			String Ids;
			foreach (String Id in this)
			{
				if (Ids != "") Ids += ",";
				Ids += Id;
			}
			return Ids;
		}

		#endregion
	}
		
	public struct PotentialDetails
	{		
		public int		PathId;		
		public String   PathName;
		public int      CandidateId;
		public DateTime InsertDate;
	}
		
	public struct Grade
	{
		public string   ExameName;
		public int      ExameId;
		public decimal  Value;
		public int		GradeId;
	}

	public struct Place
	{
		public int		PlaceId;
		public String	PlaceName;
		public String	Description;
		public int		InternetMapId;	
		public int		SmallRoomCount;
		public int		SmallRoomSits;
		public int		MediumRoomCount;
		public int		MediumRoomSits;
		public int		LargeRoomCount;
		public int		LargeRoomSits;
		public DateTime UpdateDate;
		public bool		Cancel;
		
		private Image m_imgMap;
		public Image Map { get; }

	}
	public struct SurveyField
	{	
		public int    FieldId;
		public String FieldName;
		public bool	  IsText;
		public bool   IsBool;
		public String HebrewName;
		public int	  DataId;
		public int	  NumVal;
		public String TextVal;
		public bool   BoolVal;
	}
}
